package DBLayer;
import java.sql.*;
import java.util.ArrayList;
import ModelLayer.Product;
import ModelLayer.SalesOrder;


public class DBSalesOrder implements IFDBSalesOrder {
     private  Connection con;
    public DBSalesOrder() {
       con = DBConnection.getInstance().getDBcon();
    }
   
    public SalesOrder findOrder(int salesOrderID, boolean retriveAssociation)
    {
        SalesOrder orderObj = new SalesOrder();
        orderObj = singleWhere("salesOrderID = " + salesOrderID, retriveAssociation);
        return orderObj;
    }
    private SalesOrder singleWhere(String wClause, boolean retrieveAssociation)
    {	
        ResultSet results;
        SalesOrder orderObj = new SalesOrder();
        String query =  buildQuery(wClause);
        System.out.println("DBProduct -singelWhere " + query); 
        try{ // read from department
	   Statement stmt = con.createStatement();
	   stmt.setQueryTimeout(5);
	   results = stmt.executeQuery(query);
	   int snr=0;
	   if( results.next() ){
	       orderObj = buildOrder(results);
               
           }
	   stmt.close();
	}
	catch(Exception e){
	   System.out.println("Query exception - select salesOrder : "+e);
	   
        }
	return orderObj;

    
    }
    private ArrayList<SalesOrder> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<SalesOrder> list = new ArrayList<SalesOrder>();
		
		String query =  buildQuery(wClause);
                System.out.println("DBSalesOrder " + query); 
  		try{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			int snr=0;
			while( results.next() ){
		     	 SalesOrder orderObj = new SalesOrder();
			 orderObj = buildOrder(results);	
                         list.add(orderObj);
                         
			}
			stmt.close();
		}	
	 	catch(Exception e){
	 		System.out.println("Query exception - select salesOrder : "+e);
	 	}
		return list;
	}
    private SalesOrder buildOrder(ResultSet results)
    {
       SalesOrder orderObj = new SalesOrder();
         try{
               orderObj.setSalesOrderID(results.getInt(1));
               orderObj.setSalesDate(results.getString(2));
               orderObj.setAmount(results.getDouble(3));
               orderObj.setDeliveryStatus(results.getBoolean(4));
               orderObj.setDeliveryDate(results.getString(5));
               
               System.out.println("build Order" + orderObj.getSalesOrderID());
          }
         catch(Exception e)
         {
             System.out.println("building product object");
         }
        
        return orderObj;
        
    }
 
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM SalesOrder";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}

    public ArrayList<SalesOrder> getAllOrders(boolean retriveAssociation) {
        ArrayList<SalesOrder> list = new ArrayList();
        return list;
    }

   

    public SalesOrder searchOrderOnID(int salesOrderID, boolean retriveAssociation) {
       return null;
    }
    
    public int insertSalesOrder(SalesOrder sale)
    {  
        int nextID = GetMax.getMaxId("Select max(ID) from Customer");
        nextID = nextID + 1;
        System.out.println("next ID = " +  nextID);
  
       int rc = -1;
	   String query="INSERT INTO SalesOrder (salesOrderID, salesDate, amount, deliveryStatus, deliveryDate, customerID, invoiceID)  VALUES('"+
	  		sale.getSalesOrderID()  + "','"  +
                   sale.getSalesDate()  + "','"  +
                   sale.getAmount()  + "','"  +
                   sale.isDeliveryStatus()  + "','"  +
                   sale.getDeliveryDate()  + "','"  +
                   sale.getCustomerID()  + "','"  +
                   sale.getInvoiceID()  + "','"  +
                        nextID + "','" +
                       "')";
                     
  
	   return(rc);
	}
//
	public int updateSalesOrder(SalesOrder sale)
	{
		SalesOrder saleObj  = sale;
		int rc=-1;

		String query="UPDATE Customer SET "+
		 	"name ='"+ saleObj.getSalesDate()+"', "+
                        "address ='"+ saleObj.getAmount() + "', " +
                        "zipCode ='"+ saleObj.isDeliveryStatus() + "' " +
                        "city ='"+ saleObj.getDeliveryDate() + "' " +
                        "city ='"+ saleObj.getCustomerID() + "' " +
                        "phoneNo ='"+ saleObj.getInvoiceID() + "' " +
		        " WHERE salesOrderID = '"+ saleObj.getSalesOrderID() + "'";
                System.out.println("Update query:" + query);
  		try{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);

	 	 	stmt.close();
		}
	 	catch(Exception ex){
	 	 	System.out.println("Update exception in salesOrder db: "+ex);
	  	}
		return(rc);
	}
	
	public int deleteOrder(int salesOrderID)
	{
               int rc=-1;
	  
	  	String query="DELETE FROM salesOrder WHERE salesOrderID = '" +
				salesOrderID + "'";
                System.out.println(query);
	  	try{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	        catch(Exception ex){
	 	  	System.out.println("Delete exception in salesOrder db: "+ex);
   	        }
		return(rc);
	}

   
} 